//
//  JMIPAssetsViewController.m
//  ImagePicker
//
//  Created by Jamin on 2018/10/16.
//  Copyright © 2018 jamin. All rights reserved.
//

#import "JMIPAssetsViewController.h"
#import "JMIPAssetCell.h"

@interface JMIPAssetsFlowLayout : UICollectionViewFlowLayout
- (instancetype)initWithContentSize:(CGSize)contentSize;
@end

@implementation JMIPAssetsFlowLayout
- (instancetype)initWithContentSize:(CGSize)contentSize {
    if (self = [super init])  {
        CGFloat scale = [UIScreen mainScreen].scale;
        NSInteger numberOfColumns = 4;
        CGFloat onePixel = (scale == 3.0) ? (2.0 / scale) : (1.0 / scale);
        // spacing is as small as possible
        self.minimumInteritemSpacing = onePixel;
        self.minimumLineSpacing = onePixel;
        // total spaces between items (in pixel)
        CGFloat spaces  = self.minimumInteritemSpacing * (numberOfColumns - 1);
        // item length (in pixel)
        CGFloat length  = (scale * (contentSize.width - spaces)) / numberOfColumns;
        // remaining spaces (in pixel) after rounding the length to integer
        CGFloat insets  = (length - floor(length)) * numberOfColumns;
        // round the length to integer (in pixel)
        length = floor(length);
        // divide insets to two
        CGFloat left = insets / 2;
        CGFloat right = insets / 2;
        // adjust if insets is odd
        if (fmodf(insets, 2.0) == 1.0f) {
            left -= 0.5;
            right += 0.5;
        }
        // left / right insets (in point, 2 decimal)
        left    = floorf(left / scale * 100) / 100;
        right   = floorf(right / scale * 100) / 100;
        // item length (in point, 2 decimal)
        length  = floorf(length / scale * 100) / 100;
        self.sectionInset = UIEdgeInsetsMake(0, left, 0, right);
        self.itemSize = CGSizeMake(length, length);
        self.footerReferenceSize = CGSizeMake(contentSize.width, floor(length * 2/3));
    }
    
    return self;
}

@end



#import "JMIPAlbum.h"
#import "JMImagePickerDefines.h"
@interface JMIPAssetsViewController () <
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation JMIPAssetsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.ablum.name;
    [self.view addSubview:self.collectionView];
    
    if (self.ablum.count != self.ablum.models.count) {
        [self.ablum fetchCurrentAssetsCompletion:^{
            [self.collectionView reloadData];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.ablum.models.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JMIPAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[JMIPAssetCell description] forIndexPath:indexPath];
    cell.asset = self.ablum.models[indexPath.row];
    return cell;
}


- (UICollectionView *)collectionView {
    if (!_collectionView) {
        JMIPAssetsFlowLayout *layout = [[JMIPAssetsFlowLayout alloc] initWithContentSize:self.view.bounds.size];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[JMIPAssetCell class] forCellWithReuseIdentifier:[JMIPAssetCell description]];
    }
    return _collectionView;
}


@end
